<?php
namespace app\policy\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Index extends Admin
{
    public $dept_list=[];
    public $district_list=[];

    public function __construct(){
        parent::__construct();
		$district_list =explode( ',',str_replace('，',',',trim(module_config('policy.district_list'))));
        foreach($district_list as $val)
            $this->district_list[$val]=$val;
		$dept_list =explode( ',',str_replace('，',',',trim(module_config('policy.dept_list'))));
        foreach($dept_list as $val)
            $this->dept_list[$val]=$val;
		
	}
    public function index(){
        $map = $this->getMap();
        $data_list =Db::name('policy_dept_details')
        ->where($map)
        ->paginate();
        foreach ($data_list as $key => $value) {
            if(mb_strlen($value['duty'])>10){
                $value['duty']=mb_substr($value['duty'],0,10).'...';
                $data_list[$key]=$value;
            }
            
        }
        return ZBuilder::make('table')
        ->addFilter('城区')
        ->hideCheckbox()
        ->addColumns([
            ['id', '编号'],
            ['name', '联系人'],
            ['tel', '联系电话'],
            ['url', '网址'],
            ['email','邮箱'],
            ['address', '单位地址'],
            ['duty', '服务职责'],
            ['district', '城区'],
            ['dept', '部门'],
        ])
        ->addColumn('right_button', '操作', 'btn')
        ->setColumnWidth([ 'id' => 40,'duty' => 100 ])
        ->addRightButton('edit',['href' => url('edit', ['id' => '__id__'])], ['skin' => 'layui-layer-lan'])
        ->addRightButton('delete',['href' => url('delete', ['id' => '__id__'])])
        ->addTopButton('add', ['title'=>'新增信息',], ['skin' => 'layui-layer-lan'])
        ->setRowList($data_list)
        ->fetch();
    }
    public function add(){
        return ZBuilder::make('form')
            ->setPageTitle('添加部门信息')
            ->addText('name', '联系人')
            ->addText('tel', '联系电话')
            ->addText('url', '网址')
            ->addText('email', '邮箱')
            ->addText('address', '单位地址')
            ->addTextarea('duty', '服务职责')
            ->addSelect('district', '选择城区', '', $this->district_list)
            ->addSelect('dept', '选择部门', '', $this->dept_list)
            ->setUrl(url('save'))
            ->fetch();
    }
    public function save()
    {
        if(request()->isPost()){
            $post = request()->post();
            $post['create_time']= date("Y-m-d H:i:s",time());
            Db::name('policy_dept_details')->insert($post)?$this->success('新增成功', null, '_parent_reload'):$this->error('新增失败');
        }
    }
    public function delete($id='')
    {
        Db::name('policy_dept_details')->delete($id)?$this->success('删除成功', null, '_parent_reload'):$this->error('删除失败');
    }
    public function edit($id="")
    {
        if(empty($id)){
            return ZBuilder::make('form')->assign('empty_tips', '请指定修改对象')->hideBtn(['submit', 'back'])->fetch();
        }
        $data = Db::name('policy_dept_details')->find($id);//获取编辑的数据
        return ZBuilder::make('form')
        
        ->addText('name', '联系人')
        ->addText('tel', '联系电话')
        ->addText('url', '网址')
        ->addText('email', '邮箱')
        ->addText('address', '单位地址')
        ->addTextarea('duty', '服务职责')
        ->addSelect('district', '选择城区', '', $this->district_list,$data['district'])
        ->addSelect('dept', '选择部门', '',$this->dept_list,$data['dept'])
        ->setFormData($data)
        ->setUrl(url('editSave', ['id' =>$id]))
        ->fetch();
    }

    public function editSave($id){
        $post = request()->post();
        $post['id']=$id;
        Db::name('policy_dept_details')->update($post)?$this->success('修改成功', null, '_parent_reload'):$this->error('修改失败');
    }

    public function config(){
        return module_config();
    }
}